/**
  ******************************************************************************
  * @file    dev_led.c
  * @author  Yun
  * @version V1.0.0
  * @date    2025-02-24
  * @brief   LED设备驱动模块实现
  * @license EULA (End User License Agreement)
  *          Copyright (c) 2025 星瑜科技自动化 Co., Ltd. All rights reserved.
  ******************************************************************************
  */

/*---------------------- Includes ----------------------*/
#include "dev_led.h"

/*---------------------- 函数实现 ----------------------*/
/**
  * @brief  LED设备注册函数
  */
uint8_t Dev_Led_Register(Dev_Led_Cfg_TypeDef* pstDevLedCfg,
                         void* pHwCfg,
                         Gpio_Interface_TypeDef* pstGpioInterface)
{
    /* 参数有效性检查 */
    if (!pstDevLedCfg || !pHwCfg|| !pstGpioInterface)
        return DEV_ERROR;

    DEV_UN_INIT(pstDevLedCfg);
    pstDevLedCfg->pHwCfg = pHwCfg;
    pstDevLedCfg->pstGpioInterface = pstGpioInterface;

    CHECK_DEV_STATUS(pstDevLedCfg->pstGpioInterface->Init(pstDevLedCfg->pHwCfg));

    /* 设置初始状态为关闭 */
    CHECK_DEV_STATUS(pstDevLedCfg->pstGpioInterface->Write(pstDevLedCfg->pHwCfg,
                                                           !pstDevLedCfg->u8ActState));

    DEV_INIT(pstDevLedCfg);
    return DEV_OK;
}

/**
  * @brief  开启LED
  */
uint8_t Dev_Led_On(Dev_Led_Cfg_TypeDef* pstDevLedCfg)
{
    /* 参数有效性检查 */
    if (!pstDevLedCfg) return DEV_ERROR;

    CHECK_DEV_ENABLE(pstDevLedCfg);
    DEV_BUSY(pstDevLedCfg);

    /* 写入激活状态 */
    CHECK_DEV_STATUS(pstDevLedCfg->pstGpioInterface->Write(
                                      pstDevLedCfg->pHwCfg,
                                      pstDevLedCfg->u8ActState));
    /* 推送事件通知 */
    DEV_PUSH_EVENT(EVENT_DEV_LED_ON, pstDevLedCfg, pstDevLedCfg);

    return DEV_OK;
}

/**
  * @brief  关闭LED
  */
uint8_t Dev_Led_Off(Dev_Led_Cfg_TypeDef* pstDevLedCfg)
{
    /* 参数有效性检查 */
    if (!pstDevLedCfg) return DEV_ERROR;

    CHECK_DEV_ENABLE(pstDevLedCfg);
    DEV_BUSY(pstDevLedCfg);

    /* 写入非激活状态 */
    CHECK_DEV_STATUS(pstDevLedCfg->pstGpioInterface->Write(pstDevLedCfg->pHwCfg,
                                                           !pstDevLedCfg->u8ActState));

    /* 推送事件通知 */
    DEV_PUSH_EVENT(EVENT_DEV_LED_OFF, pstDevLedCfg, pstDevLedCfg);

    return DEV_OK;
}

/**
  * @brief  翻转LED状态
  */
uint8_t Dev_Led_Toggle(Dev_Led_Cfg_TypeDef* pstDevLedCfg)
{
    /* 参数有效性检查 */
    if (!pstDevLedCfg) return DEV_ERROR;

    CHECK_DEV_ENABLE(pstDevLedCfg);
    DEV_BUSY(pstDevLedCfg);

    uint8_t u8State;
    CHECK_DEV_STATUS(pstDevLedCfg->pstGpioInterface->Read(pstDevLedCfg->pHwCfg,
                                                           &u8State));

    if(u8State == pstDevLedCfg->u8ActState)
        CHECK_DEV_STATUS(Dev_Led_Off(pstDevLedCfg));
    else
        CHECK_DEV_STATUS(Dev_Led_On(pstDevLedCfg));

    return DEV_OK;
}

/************************ (C) COPYRIGHT 星瑜科技自动化 *****END OF FILE****/
